#!/usr/bin/perl
################################################################################
#
# Runs the OSIS-to-Sword converter.
#
#   osis2Sword.pl <topLevelOutputFolder> <osisFile> <isoCode> <otherArgs>
#
# Note that this has been set up to use osis2mod as the converter, and
# assumes that this program will be co-located with the present script file.
# It will need tailoring if you wish to use another program -- see the bit
# marked !!!.
#
# It also assumes that we are running within a "standard" folder structure,
# so that the output needs to be generated in a particular location relative
# to the input.
#
# <otherArgs> contains other arguments which are passed to the converter.
# Typically (at least with osis2mod.exe), you might use this to indicate the
# versification scheme to use.
################################################################################

#-------------------------------------------------------------------------------
use strict;
use warnings;
use Cwd;
use Cwd 'abs_path';
use File::Basename;
use File::Copy;
use File::Path;
use File::Spec;



#-------------------------------------------------------------------------------
my ($outputFolder, $osisFile, $isoCode, @otherArgs) = @ARGV;
my $scriptFolder = dirname(File::Spec->rel2abs( __FILE__ ));
my $otherArgsAsString = join(" ", @otherArgs);
if ("null" eq $otherArgsAsString) { $otherArgsAsString = ""; }



#-------------------------------------------------------------------------------
if (lc($outputFolder) !~ /sword/) { die "Invalid arguments"; }



#-------------------------------------------------------------------------------
# Delete and recreate a standard folder structure.

rmtree("$outputFolder/mods.d");
rmtree("$outputFolder/modules");

mkdir("$outputFolder/mods.d");
mkdir("$outputFolder/modules");
mkdir("$outputFolder/modules/texts");
mkdir("$outputFolder/modules/texts/rawtext");
mkdir("$outputFolder/modules/texts/rawtext/$isoCode");



#-------------------------------------------------------------------------------
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# You may need to tailor the lines below for other converters.
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#-------------------------------------------------------------------------------
my $converter;
my $args = "\"$outputFolder/modules/texts/rawtext/$isoCode\" \"$osisFile\" $otherArgsAsString";
if (-e File::Spec->catfile(abs_path("$scriptFolder"), "target.location"))
{
  open FILE, "<", File::Spec->catfile(abs_path("$scriptFolder"), "target.location");
  $converter = <FILE>;
  $converter =~ s/^\s+//;
  $converter =~ s/\s+$//;
  close FILE;
}
else
{
  $converter = File::Spec->catfile(abs_path("$scriptFolder"), "osis2mod");
}



#-------------------------------------------------------------------------------
my $status = system("\"$converter\" $args");
if (0 != ($status >> 8)) { die "Conversion failed"; }



#-------------------------------------------------------------------------------
print "Processing completed successfully";
